using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AntEater.Runtime.Services.Transformation;
using System.Collections.ObjectModel;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Provides a control for displaying messages.
    /// </summary>
    public partial class MessageView : UserControl
    {

        /// <summary>
        /// Occurs when a Message is Double Clicked by the User.
        /// </summary>
        public event EventHandler<MessageClickEventArgs> MessageClick;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:MessageView"/> class.
        /// </summary>
        public MessageView() {
            InitializeComponent();

            _messages = new MessageCollection();
            _messages.Changed += new EventHandler<EventArgs>(MessagesChanged);

            this.RefreshMessageListView();
        }

        private MessageCollection _messages;

        /// <summary>
        /// Gets the messages.
        /// </summary>
        /// <value>The messages.</value>
        public MessageCollection Messages {
            get { return _messages; }
        }

        /// <summary>
        /// Called when [message clicked].
        /// </summary>
        /// <param name="message">The message.</param>
        protected void OnMessageClicked(AntEater.ComponentModel.Message message) {
            if (this.MessageClick != null) {
                this.MessageClick(this, new MessageClickEventArgs(message));
            }
        }

        /// <summary>
        /// Refreshes the message list view.
        /// </summary>
        private void RefreshMessageListView() {
            messageListView.Items.Clear();
            messageListView.Columns.Clear();
            messageListView.Groups.Clear();

            //Create Columns
            ColumnHeader columnHeader;

            //Icon
            columnHeader = new ColumnHeader();
            columnHeader.Name = "Icon";
            columnHeader.Text = "";
            messageListView.Columns.Add(columnHeader);

            //Description
            columnHeader = new ColumnHeader();
            columnHeader.Name = "Description";
            columnHeader.Text = "Description";
            messageListView.Columns.Add(columnHeader);

            //Create Groups
            string[] categories = Enum.GetNames(typeof(MessageCategory));
            foreach (string category in categories) {
                messageListView.Groups.Add(category, category);
            }

            ////Create Groups
            //ListViewGroup localBuildElementListViewGroup = new ListViewGroup("Local Build Elements");
            //ListViewGroup importedBuildElementListViewGroup = new ListViewGroup("Imported Build Elements");

            //messageListView.Groups.Add(localBuildElementListViewGroup);
            //messageListView.Groups.Add(importedBuildElementListViewGroup);

            //Create Items
            foreach (AntEater.ComponentModel.Message message in this.Messages) {
                ListViewItem item = new ListViewItem();

                item.Tag = message;
                item.ImageKey = message.Type.ToString();
                item.SubItems.Add(message.Text);
                item.Group = this.messageListView.Groups[message.Category.ToString()];

                if (message.BuildElement != null) {
                    //if (!message.BuildElement.IsImported) {
                    //    item.Group = localBuildElementListViewGroup;
                    //}
                    //else {
                    //    item.Group = importedBuildElementListViewGroup;
                    //}
                }

                        
                messageListView.Items.Add(item);
            }

            this.messageListView.Columns["Icon"].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            this.messageListView.Columns["Description"].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        /// <summary>
        /// Handles the Changed of the Messages Collection.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void MessagesChanged(object sender, EventArgs e) {
            this.RefreshMessageListView();
        }

        /// <summary>
        /// Messages the double clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void MessageDoubleClicked(object sender, EventArgs e) {
            if (this.messageListView.SelectedItems.Count == 1) {
                ListViewItem item = this.messageListView.SelectedItems[0];

                AntEater.ComponentModel.Message message = item.Tag as AntEater.ComponentModel.Message;

                if (message != null) {
                    this.OnMessageClicked(message);
                }
            }
        }
    }
}
